package CamEndzyx;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

public class ImageListener implements MouseListener, ActionListener {
    public Graphics g = null;
    public int p;
    public ImagePanel imgPanel;
    public ArrayList <BufferedImage> imglist = new ArrayList<>();
    private String name = null;
    public int flag = 1;
    public int x1,y1,x2,y2,x3,y3,x4,y4;
    public Color color;
    public MyShape[] shapeArr = new MyShape[100];
    public int index;
    boolean flag1 = false;
    //public ThreadPixel tp;

    ImageUtils imageUtils = new ImageUtils ();
    int[][] readImagePixArray = imageUtils.readImagePix ("D:\\HuaweiMoveData\\Users\\周园鑫\\Desktop\\flower.jpg");
    BufferedImage buffimg = null;
    ThreadPixel tp=null;
    @Override
    public void actionPerformed(ActionEvent e) {
        name = e.getActionCommand();

        if (name.equals ("原图")) {
            buffimg = imageUtils.drawImage_00 (readImagePixArray);
            imglist.add (buffimg);
            imgPanel.repaint();
        }else if (name.equals("撤回")) {
            imglist.remove(imglist.size() - 1);
            imgPanel.repaint();
        }else if (name.equals("马赛克")) {
            buffimg = imageUtils.drawImage_01 (readImagePixArray);
            imglist.add (buffimg);
            imgPanel.repaint();
        }else if (name.equals("灰度")) {
            buffimg = imageUtils.drawImage_02 (readImagePixArray);
            imglist.add (buffimg);
            imgPanel.repaint();
        }else if (name.equals("轮廓")) {
            buffimg = imageUtils.drawImage_03 (readImagePixArray);
            imglist.add (buffimg);
            imgPanel.repaint();
        }else if (name.equals("二值化")) {
            buffimg = imageUtils.drawImage_04 (readImagePixArray);
            imglist.add (buffimg);
            imgPanel.repaint();
        }else if (name.equals("油画")) {
            buffimg = imageUtils.drawImage_05 (readImagePixArray);
            imglist.add (buffimg);
            imgPanel.repaint();
        }else if (name.equals("美白")) {
            buffimg = imageUtils.drawImage_06 (readImagePixArray);
            imglist.add (buffimg);
            imgPanel.repaint();
        }else if (name.equals("运动模糊")) {
            buffimg = imageUtils.drawImage_07 (readImagePixArray);
            imglist.add (buffimg);
            imgPanel.repaint();
        }else if (name.equals("卷积")) {
            buffimg = imageUtils.drawImage_08 (readImagePixArray);
            imglist.add (buffimg);
            imgPanel.repaint();
        }else if (name.equals("锐化")) {
            buffimg = imageUtils.drawImage_09 (readImagePixArray);
            imglist.add (buffimg);
            imgPanel.repaint();
        }else if (name.equals("浮雕")) {
            buffimg = imageUtils.drawImage_10 (readImagePixArray);
            imglist.add (buffimg);
            imgPanel.repaint();
        }else if (name.equals("左旋")) {
            buffimg = imageUtils.drawImage_11 (readImagePixArray);
            imglist.add (buffimg);
            imgPanel.repaint();
        }else if (name.equals("右旋")) {
            buffimg = imageUtils.drawImage_12 (readImagePixArray);
            imglist.add (buffimg);
            imgPanel.repaint();
        }else if (name.equals("放大")) {
            imageUtils.drawImage_13 (readImagePixArray);
            imgPanel.repaint();
        }else if (name.equals("缩小")) {
            imageUtils.drawImage_14 (readImagePixArray);
            imgPanel.repaint();
        }else if (name.equals("打开摄像头")) {
            // 启动多线程
            // 创建线程对象,显示该线程创建一次

            if (tp == null) {
                tp = new ThreadPixel(g);
                tp.flag = true;
                // 启动线程
                tp.start();
            }
        }else if (name.equals("关闭摄像头")) {
            tp.flag = false;
        }


        if("".equals(e.getActionCommand())) {
            JButton jbu=(JButton)e.getSource();
            color=jbu.getBackground();
            g.setColor(color);
        }

        if("画点".equals(name)) {
            int xa,xb,xc,xd,ya,yb,yc,yd;
            xa=(int)(Math.random()*920+50);
            xb=(int)(Math.random()*920+50);
            xc=(int)(Math.random()*920+50);
            xd=(int)(Math.random()*920+50);
            ya=(int)(Math.random()*800+50);
            yb=(int)(Math.random()*800+50);
            yc=(int)(Math.random()*800+50);
            yd=(int)(Math.random()*800+50);

            for (int i=0;i<10000;i++) {
                p=(int)(Math.random()*900);

                if(p<300&&p>=0) {
                    g.drawLine((xa+xd)/2, (ya+yd)/2, (xa+xd)/2, (ya+yd)/2);
                    xd=(xa+xd)/2;
                    yd=(ya+yd)/2;
                }
                else if(p<600&&p>=300) {
                    g.drawLine((xb+xd)/2, (yb+yd)/2, (xb+xd)/2, (yb+yd)/2);
                    xd=(xb+xd)/2;
                    yd=(yb+yd)/2;
                }
                else {
                    g.drawLine((xc+xd)/2, (yc+yd)/2, (xc+xd)/2, (yc+yd)/2);
                    xd=(xc+xd)/2;
                    yd=(yc+yd)/2;
                }
            }
        }

        if("迭代".equals(name)) {
            double x=0,y=0;
            double a=-1.7,b=-2.5,c=-2,d=-2;
            for (int i=0;i<100000;i++) {
                x=d*Math.sin(a*x)-Math.sin(b*y);
                y=c*Math.cos(a*x)-Math.cos(b*y);
                int m=(int)((d*Math.sin(a*x)-Math.sin(b*y))*100+500);
                int n=(int)((c*Math.cos(a*x)+Math.cos(b*y))*100+350);
                g.setColor(new Color(250,i%255,i%155));
                g.drawLine(m, n, m, n);
            }
        }

        if("树".equals(name)) {
            double x=0,y=0,xn=0,yn=0;

            for (int i=0;i<100000;i++) {
                int p1=(int)(Math.random()*1000);
                if(p1<200&&p1>=0) {
                    double a=0.1950,b=-0.4880,c=0.3440,d=0.4430,ee=0.4431,f=0.2452;
                    xn=a*x+b*y+ee;
                    yn=c*x+d*y+f;
                    System.out.println("x="+xn+"  y="+yn);
                    g.drawLine((int)(900-xn*1000), (int)(900-yn*1000), (int)(900-xn*1000), (int)(900-yn*1000));
                    x=xn;
                    y=yn;
                }
                else if(p1<400&&p1>=200) {
                    double a=0.4620,b=0.4140,c=-0.2520,d=0.3610,ee=0.2511,f=0.5692;
                    xn=a*x+b*y+ee;
                    yn=c*x+d*y+f;
                    System.out.println("x="+xn+"  y="+yn);
                    g.drawLine((int)(900-xn*1000), (int)(900-yn*1000), (int)(900-xn*1000), (int)(900-yn*1000));
                    x=xn;
                    y=yn;
                }
                else if(p1<600&&p1>=400) {
                    double a=-0.6370,b=0.0000,c=0.0000,d=0.5010,ee=0.8562,f=0.2512;
                    xn=a*x+b*y+ee;
                    yn=c*x+d*y+f;
                    System.out.println("x="+xn+"  y="+yn);
                    g.drawLine((int)(900-xn*1000), (int)(900-yn*1000), (int)(900-xn*1000), (int)(900-yn*1000));
                    x=xn;
                    y=yn;
                }
                else if(p1<800&&p1>=600) {
                    double a=-0.0350,b=0.0700,c=-0.4690,d=0.0220,ee=0.4884,f=0.5069;
                    xn=a*x+b*y+ee;
                    yn=c*x+d*y+f;
                    System.out.println("x="+xn+"  y="+yn);
                    g.drawLine((int)(900-xn*1000), (int)(900-yn*1000), (int)(900-xn*1000), (int)(900-yn*1000));
                    x=xn;
                    y=yn;
                }
                else {
                    double a=-0.0580,b=-0.0700,c=0.4530,d=-0.1110,ee=0.5976,f=0.0969;
                    xn=a*x+b*y+ee;
                    yn=c*x+d*y+f;
                    System.out.println("x="+xn+"  y="+yn);
                    g.drawLine((int)(900-xn*1000), (int)(900-yn*1000), (int)(900-xn*1000), (int)(900-yn*1000));
                    x=xn;
                    y=yn;
                }
            }
        }

    }

    @Override
    public void mouseClicked(MouseEvent e) {
        x4=e.getX();
        y4=e.getY();

        if("特殊三角形".equals(name)&&flag==2) {
            g.drawLine(x1, y1, x4, y4);
            g.drawLine(x2, y2, x4, y4);
            flag = 1;

            MyShape shape1 = new MyShape();
            shape1.name = name;
            shape1.color = color;
            shape1.x1 = x1;
            shape1.x2 = x4;
            shape1.y1 = y1;
            shape1.y2 = y4;
            shapeArr[index++] = shape1;

            MyShape shape2 = new MyShape();
            shape2.name = name;
            shape2.color = color;
            shape2.x1 = x4;
            shape2.x2 = x2;
            shape2.y1 = y4;
            shape2.y2 = y2;
            shapeArr[index++] = shape2;

        }

        if("多边形".equals(name)&&flag==2) {
            if(!flag1) {
                x3=x2;
                y3=y2;
                flag1=true;
            }

            g.drawLine(x3, y3, x4, y4);

            MyShape shape3 = new MyShape();
            shape3.name=name;
            shape3.color=color;
            shape3.x1=x4;
            shape3.x2=x3;
            shape3.y1=y4;
            shape3.y2=y3;
            shapeArr[index++]=shape3;

            x3=x4;
            y3=y4;

            if(e.getClickCount() == 2) {
                g.drawLine(x1, y1, x4, y4);
                flag=1;

                MyShape shape4 = new MyShape();
                shape4.name=name;
                shape4.color=color;
                shape4.x1=x4;
                shape4.x2=x1;
                shape4.y1=y4;
                shape4.y2=y1;
                shapeArr[index++]=shape4;

            }
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

        if(flag==1){
            x1=e.getX();
            y1=e.getY();
        }

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        System.out.println("x1="+x1+";y1="+y1+";x2="+x2+";y2="+y2);
        if(flag==1){
            x2=e.getX();
            y2=e.getY();
        }

        if("直线".equals(name)){
            g.drawLine(x1, y1, x2, y2);
        }

        if("矩形".equals(name)){
            g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1));
        }

        if("椭圆".equals(name)){
            g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1));
        }

        if("三角形".equals(name)){
            int[]xPoints=new int[] {x1,(x1+x2)/2,x2};
            int[]yPoints=new int[] {y2,y1,y2};
            int nPoints=3;
            g.drawPolygon(xPoints, yPoints, nPoints);
        }

        if("正方形".equals(name)){
            g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(x2-x1));
        }

        if("菱形".equals(name)){
            int[]xPoints=new int[] {(x1+x2)/2,x2,(x1+x2)/2,x1};
            int[]yPoints=new int[] {y2,(y1+y2)/2,y1,(y1+y2)/2};
            int nPoints=4;
            g.drawPolygon(xPoints, yPoints, nPoints);
        }

        if("特殊三角形".equals(name)&&flag==1){
            g.drawLine(x1,y1,x2,y2);
            flag++;
        }

        if("多边形".equals(name)&&flag==1){
            g.drawLine(x1,y1,x2,y2);
            flag++;
        }

        MyShape shape = new MyShape();
        shape.name = name;
        shape.color = color;
        shape.x1 = x1;
        shape.x2 = x2;
        shape.y1 = y1;
        shape.y2 = y2;
        shapeArr[index++] = shape;
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}